﻿using UnityEngine;
using System.Collections;
using System.IO;

public class bridgeScene : MonoBehaviour
{
    int photoID = 0;
    HelpURLAttribute[] urls;

    // Use this for initialization
    void Start()
    {
        HelpURLAttribute url = new HelpURLAttribute("http://api.map.baidu.com/panorama/v2?ak=uwhciGdsgvcAvzybc6Up1S4Q&width=1024&height=512&location=114.294359,30.555754&fov=360");
        StartCoroutine(LoadImg(url));
    }

    // Update is called once per frame
    void Update()
    {

    }

    IEnumerator LoadImg(HelpURLAttribute url)
    {
        //开始下载图片
        WWW www = new WWW(url.URL);
        yield return www;

        //下载完成，保存图片到路径filePath
        Texture2D texture = www.texture;
        //byte[] bytes = texture.EncodeToPNG();
        //File.WriteAllBytes(Application.dataPath + "/Resources/Map/Bridge.jpg", bytes);

        GameObject.FindGameObjectWithTag("Sphere").GetComponent<Renderer>().material.mainTexture = texture;
    }

    public void PhotoAdd()
    {
        urls[0] = new HelpURLAttribute("http://api.map.baidu.com/panorama/v2?ak=uwhciGdsgvcAvzybc6Up1S4Q&width=1024&height=512&location=114.294359,30.555754&fov=360");
        try
        {
            StartCoroutine(LoadImg(urls[++photoID]));
        }
        catch
        {
            photoID = urls.Length;
        }
    }

    public void PhotoMinus()
    {
        try
        {
            StartCoroutine(LoadImg(urls[--photoID]));
        }
        catch
        {
            photoID = 0;
        }
    }
}
